<template>
  <div class="search-result">
    <van-list v-model="loading" @load="getResults">
      <van-cell
        v-for="item in results"
        :key="item.art_id"
        :title="item.title"
        :finished="finished"
        finished-text="没有更多搜索结果"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
      ></van-cell>
    </van-list>
  </div>
</template>

<script>
import { getResultsAPI } from "@/api";
export default {
  data() {
    return {
      loading: false,
      results: [],
      page: 1,
      finished: false,
      error: false,
    };
  },

  methods: {
    async getResults() {
      // console.log(this.$parent.keyWords);
      try {
        //1.发送请求获取数据
        const { data } = await getResultsAPI(
          this.$parent.keyWords,
          this.page++,
        );
        //2.添加数据
        this.results.push(...data.data.results);

        //3.判断是否还有数据
        if (data.data.results.length === 0) {
          this.finished = true;
        }
      } catch (error) {
        this.error = true;
      } finally {
        //4.将loading设置为false
        this.loading = false;
      }
    },
  },
};
</script>

<style scoped lang="less">
.search-result {
  height: calc(100vh - 108px);
  overflow: auto;
}
</style>